package com.qyhl.webtv.module_news.news.information.info;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.news.SectionBean;
import com.qyhl.webtv.commonlib.service.BrokeService;
import com.qyhl.webtv.commonlib.utils.config.HomeThemeColorUtil;
import com.qyhl.webtv.module_news.news.information.info.fragment.InformationFragment;
import com.umeng.analytics.MobclickAgent;
import com.weavey.loading.lib.LoadingLayout;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by qyhl on 2017/11/28.
 * 资讯界面
 */

@Route(path = ARouterPathConstant.INFO)
public class InformationActivity extends BaseActivity implements InformationContract.InformationView {
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.tabLayout)
    SlidingTabLayout mTabLayout;
    @BindView(R2.id.viewPager)
    ViewPager mViewPager;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.back_btn)
    ImageView backBtn;

    private InformationPresenter mPresenter;
    @Autowired(name = "title")
    String mTitle;
    @Autowired(name = "id")
    String id;
    @Autowired(name = "type")
    String type;
    private List<SectionBean> mList;

    // Frgaments
    private List<Fragment> mFragmentList;

    @Autowired(name = ServicePathConstant.BROKE_SERVICE)
    public BrokeService brokeService;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);

    }

    @Override
    protected int getContentView() {
        return R.layout.news_activity_information;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new InformationPresenter(this);
        initView();
    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getData(id, type);
    }

    /**
     * 初始化
     */
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        mList = new ArrayList<>();
        mFragmentList = new ArrayList<>();

        title.setText(mTitle == null ? "资讯" : mTitle);

    }

    /**
     * 设置监听
     */
    private void setListeners() {
        //重新加载
        loadMask.setOnReloadListener(v -> {
            loadMask.setReloadButtonText("加载中...");
            mPresenter.getData(id, type);
        });

        backBtn.setOnClickListener(v -> finish());
    }

    /**
     * 设置栏目
     *
     * @param beanList
     */
    @Override
    public void setData(List<SectionBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~~");
        mList = beanList;

        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getMenuType() == 3) {
                mFragmentList.add(brokeService.getFragment(true, (int) mList.get(i).getSectionId()));
            } else {
                mFragmentList.add(InformationFragment.newInstance(mList.get(i).getSectionId() + ""));
            }
        }
        //PageAdapter
        InformationPageAdapter mViewPageadapter = new InformationPageAdapter(getSupportFragmentManager(), mFragmentList);
        mViewPageadapter.setData(mList);
        mViewPager.setAdapter(mViewPageadapter);
        try {
            Class<? extends SlidingTabLayout> aClass = mTabLayout.getClass();
            Field f = aClass.getDeclaredField("mTabPadding");
            f.setAccessible(true);
            f.set(mTabLayout, StringUtils.dip2px(this, 15));
        } catch (Exception e) {
            e.printStackTrace();
        }

        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setTextSelectColor(HomeThemeColorUtil.getCheckColor());
        mTabLayout.setTextUnselectColor(HomeThemeColorUtil.getNormalColor());
        mTabLayout.getTitleView(0).getPaint().setFakeBoldText(true);
        mViewPageadapter.notifyDataSetChanged();
    }

    /**
     * 无内容
     *
     * @param msg
     */
    @Override
    public void setEmpty(String msg) {
        loadMask.setEmptyText(msg);
        loadMask.setStatus(LoadingLayout.Empty);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 出错
     *
     * @param msg
     */
    @Override
    public void setError(String msg) {
        loadMask.setErrorText(msg);
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 网络异常
     *
     * @param msg
     */
    @Override
    public void setNetError(String msg) {
        loadMask.setNoNetworkText(msg);
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setReloadButtonText("点击重试~~");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("资讯");
        MobclickAgent.onResume(this);
    }


    @Override
    protected void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("资讯");
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
